package com.lysj.bhp.exchangerate.service;

import com.lysj.bhp.exchangerate.base.ExchangeRateContent;
import com.lysj.bhp.exchangerate.domain.ExchangeRate;
import com.lysj.bhp.util.random.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 汇率相关服务类
 */
@Service
public class ExchangeRateService extends ExchangeRateContent {

    private static Logger logger = LoggerFactory.getLogger(ExchangeRateService.class);

    private static final long FIVE_MINUTES = 1000 * 60 * 5;

    private static final long TEN_MINUTES = 1000 * 60 * 10;

    /**
     * 获取以太坊币与美元的汇率
     *
     * @return
     */
    public BigDecimal getEthereumToDollar() {
        if (System.currentTimeMillis() < ExchangeRate.ETHEREUM_TO_DOLLAR_EXPIRES) { //汇率还在有效期内
            return ExchangeRate.ETHEREUM_TO_DOLLAR;
        }
        BigDecimal vessel = ExchangeRate.ETHEREUM_TO_DOLLAR;
        if (1 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexService.getEthereumToDollar();
        } else if (2 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexproxyService.getEthereumToDollar();
        } else if (3 == ExchangeRate.DATA_RESOURCE) {
            vessel = digifinexService.getEthereumToDollar();
        }
        if(new BigDecimal("-1").compareTo(vessel) != 0){
            ExchangeRate.ETHEREUM_TO_DOLLAR = vessel;
        }
        ExchangeRate.ETHEREUM_TO_DOLLAR_EXPIRES = System.currentTimeMillis() + RandomUtil.getRandomNum(FIVE_MINUTES, TEN_MINUTES);
        return ExchangeRate.ETHEREUM_TO_DOLLAR;
    }


    /**
     * @author drj
     * @date 2018/07/31 21:16
     * @Description:比特币对美元的汇率
     */
    public BigDecimal getBitcoinToDollar() {
        if (System.currentTimeMillis() < ExchangeRate.BITCOIN_TO_DOLLAR_EXPIRES) { //汇率还在有效期内
            return ExchangeRate.BITCOIN_TO_DOLLAR;
        }
        BigDecimal vessel = ExchangeRate.BITCOIN_TO_DOLLAR;
        if (1 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexService.getBitcoinToDollar();
        } else if (2 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexproxyService.getBitcoinToDollar();
        } else if (3 == ExchangeRate.DATA_RESOURCE) {
            vessel = digifinexService.getBitcoinToDollar();
        }
        if(new BigDecimal("-1").compareTo(vessel) != 0){
            ExchangeRate.BITCOIN_TO_DOLLAR = vessel;
        }
        ExchangeRate.BITCOIN_TO_DOLLAR_EXPIRES = System.currentTimeMillis() + RandomUtil.getRandomNum(FIVE_MINUTES, TEN_MINUTES);
        return ExchangeRate.BITCOIN_TO_DOLLAR;
    }


    /**
     * @author drj
     * @date 2018/05/30 20:06
     * @Description:获取人民币与美元的汇率
     */
    public BigDecimal cnyToDollarRate() {
        if (System.currentTimeMillis() < ExchangeRate.CNY_TO_DOLLAR_EXPIRES) { //汇率还在有效期内
            return ExchangeRate.CNY_TO_DOLLAR;
        }
        BigDecimal vessel = ExchangeRate.CNY_TO_DOLLAR;
        if (1 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexService.cnyToDollarRate();
        } else if (2 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexproxyService.cnyToDollarRate();
        } else if (3 == ExchangeRate.DATA_RESOURCE) {
            vessel = digifinexService.cnyToDollarRate();
        }
        if(new BigDecimal("-1").compareTo(vessel) != 0){
            ExchangeRate.CNY_TO_DOLLAR = vessel;
        }
        ExchangeRate.CNY_TO_DOLLAR_EXPIRES = System.currentTimeMillis() + RandomUtil.getRandomNum(FIVE_MINUTES, TEN_MINUTES);
        return ExchangeRate.CNY_TO_DOLLAR;
    }
    /**
     * 算力币与美元的汇率
     *
     * @return
     */
    public BigDecimal getBhpcToDollar() {
        if (System.currentTimeMillis() < ExchangeRate.BHPC_TO_DOLLAR_EXPIRES) { //汇率还在有效期内
            return ExchangeRate.BHPC_TO_DOLLAR;
        }
        BigDecimal vessel = ExchangeRate.BHPC_TO_DOLLAR;
        if (1 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexService.getBhpcToDollar();
        } else if (2 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexproxyService.getBhpcToDollar();
        } else if (3 == ExchangeRate.DATA_RESOURCE) {
            vessel = digifinexService.getBhpcToDollar();
        }
        if(new BigDecimal("-1").compareTo(vessel) != 0){
            ExchangeRate.BHPC_TO_DOLLAR = vessel;
        }
        ExchangeRate.BHPC_TO_DOLLAR_EXPIRES = System.currentTimeMillis() + RandomUtil.getRandomNum(FIVE_MINUTES, TEN_MINUTES);
        return ExchangeRate.BHPC_TO_DOLLAR;
    }

    /**
     * @author zwj
     * @date 2018-08-01 16:53
     * @Description:比特砖石对美元
     */
    public BigDecimal getDiamoundToDollar() {
        if (System.currentTimeMillis() < ExchangeRate.DIAMOUND_TO_DOLLAR_EXPIRES) { //汇率还在有效期内
            return ExchangeRate.DIAMOUND_TO_DOLLAR;
        }
        BigDecimal vessel = ExchangeRate.DIAMOUND_TO_DOLLAR;
        if (1 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexService.getDiamoundToDollar();
        } else if (2 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexproxyService.getDiamoundToDollar();
        } else if (3 == ExchangeRate.DATA_RESOURCE) {
            vessel = digifinexService.getDiamoundToDollar();
        }
        if(new BigDecimal("-1").compareTo(vessel) != 0){
            ExchangeRate.DIAMOUND_TO_DOLLAR = vessel;
        }
        ExchangeRate.DIAMOUND_TO_DOLLAR_EXPIRES = System.currentTimeMillis() + RandomUtil.getRandomNum(FIVE_MINUTES, TEN_MINUTES);
        return ExchangeRate.DIAMOUND_TO_DOLLAR;
    }

    /**
     * @author zwj
     * @date 2018-08-02 09:32
     * @Description:布比对美元
     */
    public BigDecimal getBuToDollar() {
        if (System.currentTimeMillis() < ExchangeRate.BU_TO_DOLLAR_EXPIRES) { //汇率还在有效期内
            return ExchangeRate.BU_TO_DOLLAR;
        }
        BigDecimal vessel = ExchangeRate.BU_TO_DOLLAR;
        if (1 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexService.getBuToDollar();
        } else if (2 == ExchangeRate.DATA_RESOURCE) {
            vessel = bcexproxyService.getBuToDollar();
        } else if (3 == ExchangeRate.DATA_RESOURCE) {
            vessel = digifinexService.getBuToDollar();
        }
        if(new BigDecimal("-1").compareTo(vessel) != 0){
            ExchangeRate.BU_TO_DOLLAR = vessel;
        }
        ExchangeRate.BU_TO_DOLLAR_EXPIRES = System.currentTimeMillis() + RandomUtil.getRandomNum(FIVE_MINUTES, TEN_MINUTES);
        return ExchangeRate.BU_TO_DOLLAR;
    }

    /**
     * 获取BHPC与人民币的汇率
     *
     * @return
     */
    public BigDecimal bhpcToCnyRate() {
        BigDecimal bhpcToCnyRate = getBhpcToDollar().divide(cnyToDollarRate(), 6, BigDecimal.ROUND_DOWN);
        return bhpcToCnyRate;
    }


    /**
     * @author wtl
     * @date 2018-06-07 19:33
     * @Description:获取比特币对人民币的汇率
     */
    public BigDecimal bitcoinToCnyRate() {
        BigDecimal bitcoinToCnyRate = getBitcoinToDollar().divide(cnyToDollarRate(), 6, BigDecimal.ROUND_DOWN);
        return bitcoinToCnyRate;
    }


    /**
     * @author wtl
     * @date 2018-06-07 19:33
     * @Description:获取以太坊对人民币的汇率
     */
    public BigDecimal ethToCnyRate() {
        BigDecimal ethToCnyRate = getEthereumToDollar().divide(cnyToDollarRate(), 6, BigDecimal.ROUND_DOWN);
        return ethToCnyRate;
    }


    /**
     * @author drj
     * @date 2018/07/31 22:33
     * @Description:美元对人民币的汇率
     */
    public BigDecimal dollarToCnyRate() {
        BigDecimal dollarToCnyRate = new BigDecimal(1).divide(cnyToDollarRate(), 6, BigDecimal.ROUND_DOWN);
        return dollarToCnyRate;
    }

    /**
     * @author drj
     * @date 2018/07/31 22:33
     * @Description:比特币砖石对人民币的汇率
     */
    public BigDecimal diamoundToCnyRate() {
        BigDecimal diamoundToCnyRate = getDiamoundToDollar().divide(cnyToDollarRate(), 6, BigDecimal.ROUND_DOWN);
        return diamoundToCnyRate;
    }

    /**
     * @author drj
     * @date 2018/07/31 22:33
     * @Description:布比币对人民币的汇率
     */
    public BigDecimal buToCnyRate() {
        BigDecimal buToCnyRate = getBuToDollar().divide(cnyToDollarRate(), 6, BigDecimal.ROUND_DOWN);
        return buToCnyRate;
    }

    /**
     * @author zwj
     * @date 2018-09-28 14:06
     * @@Description:链币种转CNY 10:btc,20:eth,100:bhpc
     */
    public BigDecimal qtumMoneyToCny(Integer type,BigDecimal qtumMoney){
        BigDecimal cnyMoney = BigDecimal.ZERO;
        switch (type){
            case 10:
                cnyMoney = qtumMoney.multiply(bitcoinToCnyRate());
                break;
            case 20:
                cnyMoney = qtumMoney.multiply(ethToCnyRate());
                break;
            case 100:
                cnyMoney = qtumMoney.multiply(bhpcToCnyRate());
                break;
        }
        return cnyMoney;
    }


}
